package com.wteam.dragon.system.apps.security.config.provide;

import com.wteam.dragon.system.apps.security.config.token.OpenidAuthenticationToken;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

/**
 * @Author 陈晓辉
 * @Date 2021/3/31 8:58
 */
@Data
@Component
@RequiredArgsConstructor
public class OpenidAuthenticationProvide implements AuthenticationProvider {
    private final UserDetailsService userDetailsService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String openid = (String)authentication.getPrincipal();
        UserDetails userDetails = userDetailsService.loadUserByUsername(openid);
        return new OpenidAuthenticationToken(userDetails, userDetails.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return OpenidAuthenticationToken.class.isAssignableFrom(authentication);
    }
}
